/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.cef.command.listener;

import cef.command.CommandI;
import cef.command.listener.CommandOutputEvent;
import cef.command.listener.CommandOutputListener;
import cef.command.listener.CommandOutputListenerException;
import cef.command.listener.CommandPrintWriter;
import coalescent.cef.command.MLE_KC64ThetaCommand;
import commons.util.FormatUtil;
import commons.util.MapUtil;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @version version
 * @user Susanta Tewari
 * @history First created on 10/31/12.
 * @since 1.4.0
 */
public final class MLE_KC64ThetaCommandPrintWriter extends CommandPrintWriter {

    /**
     * Method description
     *
     * @param cmdEvent description
     *
     * @throws CommandOutputListenerException description
     */
    @Override
    public void receivedEventImpl(final CommandOutputEvent cmdEvent)
            throws CommandOutputListenerException {

        if (cmdEvent.getSource() instanceof MLE_KC64ThetaCommand) {

            final MLE_KC64ThetaCommand command = (MLE_KC64ThetaCommand) cmdEvent.getSource();


            // MLE
            // theta probabilities map
            final Map<Double, BigDecimal> thetaProbabilities = command.getThetaProbabilities();


            // map sorted
            final Map<Double, BigDecimal> map =
                new LinkedHashMap<Double,
                                  BigDecimal>(MapUtil.sortByValue(thetaProbabilities,
                                      Collections.reverseOrder()));


            // get the MLE
            final double mle          = map.keySet().iterator().next();
            final BigDecimal mle_prob = map.get(mle);


            // print MLE
            getPrintWriter().println("MLE: " + FormatUtil.format(mle) + " Likelihood: "
                                     + FormatUtil.format(mle_prob));
            getPrintWriter().flush();
        }
    }

    /**
     * Method description
     *
     * @return description
     */
    @Override
    public CommandOutputListener newInstance() {
        return new MLE_KC64ThetaCommandPrintWriter();
    }

    /**
     * Method description
     *
     * @return description
     */
    @Override
    public Class<? extends CommandI> getTargetCommandClass() {
        return MLE_KC64ThetaCommand.class;
    }
}
